Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple issues with converting type hints to JSON Schema #2379

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

kepler
Copy link
Contributor

@kepler kepler commented Mar 12, 2025

Description

Summary of changes:

The main problem: newish syntax for unions with | supported by Python 3.10 are of type types.UnionType, and didn't match Union. There's a very recently completed issue on CPython that will unify both (apparently for 3.14). See also this MyPy warning about evaluating type hints.

Other problems:

  • Only unions of two values were properly handled for optional values;
  • Unions with more than two values and an optional one would only parse one of the values, dropping the others;
  • Some code lines were never true (like isinstance(arg_json_schema["type"], list));

The code is now simplified while handling new type annotation syntax and null/optional values.

  • Related issues: I didn't find any.
  • Motivation and context: I was getting errors with a custom toolkit that uses the new type hinting syntax.
  • Environment or dependencies: None.
  • Impact on metrics: N/A.

Type of change

Please check the options that are relevant:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Model update (Addition or modification of models)
  • Other (please describe):

Checklist

  • Adherence to standards: Code complies with Agno’s style guidelines and best practices.
  • Formatting and validation: You have run ./scripts/format.sh and ./scripts/validate.sh to ensure code is formatted and linted.
  • Self-review completed: A thorough review has been performed by the contributor(s).
  • Documentation: Docstrings and comments have been added or updated for any complex logic.
  • Examples and guides: Relevant cookbook examples have been included or updated (if applicable).
  • Tested in a clean environment: Changes have been tested in a clean environment to confirm expected behavior.
  • Tests (optional): Tests have been added or updated to cover any new or changed functionality.

Additional Notes

Linked in the description.

@kepler kepler requested a review from a team as a code owner March 12, 2025 18:45
kepler added 2 commits March 13, 2025 09:49
The main issue: newish syntax for unions with `|` supported by Python 3.10 are of [type](https://docs.python.org/3.10/library/types.html#types.UnionType) `types.UnionType`, and didn't match `type(Union)`. There's a very recently [completed issue on CPython](python/cpython#105499) that will unify both (apparently for 3.14). See also this [MyPy warning](https://mypy.readthedocs.io/en/stable/runtime_troubles.html#future-annotations-import-pep-563) about evaluating type hints.

Other issues:
- Only unions of two values were properly handled for optional values;
- Unions with more than two values and an optional one would only parse one of the values, dropping the others;
- Some code lines were never true (like `isinstance(arg_json_schema["type"], list)`);

The code is now simplified while handling new type annotation syntax and null/optional values.
@kepler kepler force-pushed the fix-json-schema-util branch from b309c5f to e93d024 Compare March 13, 2025 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant